package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.EmpQueryParam;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpExprService empExprService;
    @Autowired
    private DeptService deptService;
    @Override
    @Transactional
    public void saveEmpAndEmpExpr(Emp emp) {
        // 保存员工信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        this.save(emp);
        // 保存员工工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr ->expr.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
    }

    @Override
    public Result queryPage(EmpQueryParam param) {
        //分页
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());
        //查询员工
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();
        LambdaQueryWrapper<Emp> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotEmpty(name),Emp::getName,name)
                .ge(ObjectUtils.isNotNull(gender),Emp::getGender,gender)
                .between(ObjectUtils.isEmpty(begin)&&ObjectUtils.isNotNull(end),Emp::getEntryDate,begin,end);
        this.page(page,queryWrapper);
        //查询部门名称
        List<Emp> emps = page.getRecords();
        Set<Integer> deptIds = emps.stream().map(Emp::getDeptId).collect(Collectors.toSet());
        if (!deptIds.isEmpty()){
            List<Dept> depts = deptService.listByIds(deptIds);
            Map<Integer, String> deptsMap = depts.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
            emps.forEach(emp -> emp.setDeptName(deptsMap.get(emp.getDeptId())));
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return Result.success(pageResult);
    }

    @Override
    @Transactional
    public void updateEmp(Emp emp) {
        //更新员工
        emp.setUpdateTime(LocalDateTime.now());
       this.updateById(emp);
       //删除员工工作经历
         empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,emp.getId()));
       //更新员工工作经历
        List<EmpExpr> exprList = emp.getExprList();
         if (exprList != null&&!exprList.isEmpty()){
             exprList.forEach(expr ->expr.setEmpId(emp.getId()));
         }

        empExprService.saveBatch(exprList);


    }


}
